<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\MillRebateIncome\RebateIncomeBatchGrid;
use App\Admin\Actions\Grid\Withdraw\SendTx;
use App\Admin\Actions\Grid\Withdraw\SendTxBatchGrid;
use App\Admin\Actions\Grid\Withdraw\WithdrawCheckGrid;
use App\Admin\Actions\Grid\Withdraw\WithdrawStateGrid;
use App\Admin\Actions\Grid\Withdraw\WithdrawSuccessGrid;
use App\Admin\Repositories\UserWithdraw;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;

class UserWithdrawController extends AdminBaseController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserWithdraw(['user']), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $this->gridFilter($grid, 'user_withdraws');
            $grid->column('id')->sortable();
            $grid->column('uid');
//            $grid->column('uid')->display(function ($uid) {
//                return ($this->user && $this->user->name ? "{$this->user->name}<br/>" : "") . "ID:{$uid}";
//            });

            $grid->column("info", "金额")->display(function () {
                $from_amount = asMoney($this->from_amount);
                $fee = asMoney($this->fee);
                $real_amount = asMoney($this->real_amount);
                $msg = "金额:{$from_amount}<br>手续费:{$fee}<br> 实际到账{$real_amount}";
                return $msg;

            });

//            $grid->column('from_symbol');
//            $grid->column('from_amount')->numberFormat();
//            $grid->column('to_symbol');
//            $grid->column('to_amount')->numberFormat();
////            $grid->column('rate')->numberFormat();
//            $grid->column('real_amount')->numberFormat();
//            $grid->column('from_address');
            $grid->column('to_address')
                ->if(function (Grid\Column $to_address) {
                    return strlen($to_address->getValue()) > 16;
                })->qrcode();
            $grid->column('status')->using(tt_trade_status('', true))->label([
                TRADE_STATUS_UNTREATED => 'primary',
                TRADE_STATUS_SUCCESS => 'success',
                TRADE_STATUS_FAIL => 'danger',
            ]);

            $grid->column('tx_status', "链状态")->using(tt_tx_status('', true))->label([
                TX_STATUS_UNTREATED => 'primary',
                TX_STATUS_PROCESSING => 'warning',
                TX_STATUS_SEND => 'info',
                TX_STATUS_SUCCESS => 'success',
                TX_STATUS_FAIL => 'danger',
            ]);
//            $grid->column('fee_symbol');
//            $grid->column('fee')->numberFormat();
            $grid->column('remark');
            $grid->column('executed_at');
            $grid->column('executed_tx')->link(function ($v) {
                if ($v) {
                    $v = str_replace("0x", "", $v);
                    return "https://tronscan.org/#/transaction/$v";
                }
            });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();


            $grid->disableViewButton();
            $grid->disableEditButton();

            if (Admin::user()->can('modify')) {
                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    if ($this->status == TRADE_STATUS_UNTREATED && ($this->tx_status == TX_STATUS_FAIL || $this->tx_status == TX_STATUS_UNTREATED)) {
                        $actions->append(new WithdrawCheckGrid());
//                    $success = new WithdrawStateGrid();
//                    $success->setInput(['status' => TRADE_STATUS_SUCCESS]);
//                    $actions->append($success);
//                    $fail = new WithdrawStateGrid();
//                    $fail->setInput(['status' => TRADE_STATUS_FAIL]);
//                    $actions->append($fail);
                        $actions->append(new SendTx());
                    } else if ($this->tx_status == TX_STATUS_SEND && $this->executed_tx != "") {
                        $actions->append(new WithdrawSuccessGrid());
                    }
                });
            }

            $grid->disableRowSelector(false);
            $grid->disableBatchActions(false);
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->add(new SendTxBatchGrid("发送交易"));
                    $batch->disableDelete();
                });
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id')->width(3);
                $filter->equal('uid')->width(3);
                $filter->equal('to_address')->width(3);
                $filter->equal('executed_tx')->width(3);
                $filter->equal('status')->width(3)->select(tt_trade_status('', true));
                $filter->equal('tx_status', "链状态")->width(3)->select(tt_tx_status('', true));
//                $filter->equal('from_symbol')->width(3);
//                $filter->equal('to_symbol')->width(3);
                $filter->between('created_at')->datetime()->width(6);

            });

            $grid->disableCreateButton();

            $titles = ['uid' => "用户id", 'from_amount' => '金额', 'fee' => '手续费', 'real_amount' => '到账金额', 'to_address' => '转出地址',
                'status' => '状态', 'executed_tx' => '哈希', 'created_at' => '创建时间'];
            $grid->export($titles);
            $grid->export()->titles($titles);
            $grid->export()->csv();
            $grid->export()->rows(function ($rows) {
                foreach ($rows as $index => &$row) {
                    $row['status'] = tt_trade_status($row['status']);
                }
                return $rows;
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserWithdraw(), function (Show $show) {
            $show->field('id');
            $show->field('uid');
            $show->field('from_symbol');
            $show->field('from_amount');
            $show->field('to_symbol');
            $show->field('to_amount');
            $show->field('rate');
            $show->field('from_address');
            $show->field('to_address');
            $show->field('status');
            $show->field('fee_symbol');
            $show->field('fee');
            $show->field('remark');
            $show->field('executed_at');
            $show->field('executed_tx');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserWithdraw(), function (Form $form) {
            $form->display('id');
            $form->text('uid');
            $form->text('from_symbol');
            $form->text('from_amount');
            $form->text('to_symbol');
            $form->text('to_amount');
            $form->text('rate');
            $form->text('from_address');
            $form->text('to_address');
            $form->text('status');
            $form->text('fee_symbol');
            $form->text('fee');
            $form->text('remark');
            $form->text('executed_at');
            $form->text('executed_tx');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
